package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/19.N
 */

public class TeamMaintainOrderListModel implements InTeamMaintainOrderListModel {

    @Override
    public void getTeamMaintainOrderList(final OnTeamMaintainOrderListener teamMaintainOrderListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(ProjectUtil.TEAM_MAINTAIN_ORDER_LIST);

                HttpResponse response;
                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != teamMaintainOrderListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("engineeringDeptOrderList"));
                            List<OrderParentBean> engineeringOrderBeanList = new ArrayList<OrderParentBean>();
                            if (jsonArr != null) {

                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        OrderParentBean orderBean = new OrderParentBean();
                                        orderBean.setId(jsonObj.getString("id"));
                                        String category = jsonObj.getString("category");
                                        if (null != category) {
                                            if (category.equals("INSTALL")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.INSTALL);
                                            } else if (category.equals("BATCH_INSTALL")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.BATCH_INSTALL);
                                            } else if (category.equals("CONSTRUCT")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.CONSTRUCT);
                                            } else if (category.equals("MAINTAIN")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.MAINTAIN);
                                            } else if (category.equals("PUBLICATION")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.PUBLICATION);
                                            } else if (category.equals("PATROL")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.PATROL);
                                            }
                                        }


                                        String completeStatus = jsonObj.getString("completeStatus");
                                        if (null != completeStatus) {
                                            if (completeStatus.equals("WORKING")) {
                                                orderBean.setCompleteStatus(CommonEnum.CompleteStatus.WORKING);
                                            } else if (completeStatus.equals("WORKER_COMPLETE")) {
                                                orderBean.setCompleteStatus(CommonEnum.CompleteStatus.WORKER_COMPLETE);
                                            } else if (completeStatus.equals("ADMIN_COMPLETE")) {
                                                orderBean.setCompleteStatus(CommonEnum.CompleteStatus.ADMIN_COMPLETE);
                                            } else if (completeStatus.equals("SUPERADMIN_COMPLETE")) {
                                                orderBean.setCompleteStatus(CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE);
                                            }
                                        }


                                        orderBean.setEngineeringName(jsonObj.getString("engineeringName"));
                                        orderBean.setProjectName(jsonObj.getString("projectName"));
                                        String sendOrderTime = jsonObj.getString("sendOrderTime");
                                        if (!TextUtils.isEmpty(sendOrderTime))
                                            orderBean.setSendOrderTime(Long.parseLong(sendOrderTime));


                                        orderBean.setCreateDate(jsonObj.getLong("createDate"));
                                        orderBean.setHasPauseOrder(jsonObj.getBoolean("hasPauseOrder"));

                                        String status = jsonObj.getString("status");
                                        if (null != status) {
                                            if (status.equals("CREATED")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.CREATED);
                                            } else if (status.equals("EDITED")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.EDITED);

                                            } else if (status.equals("SEND")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.SEND);

                                            } else if (status.equals("RECEIVED")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RECEIVED);
                                            } else if (status.equals("RETURNED")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RETURNED);
                                            } else if (status.equals("CONSTRUCT")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.CONSTRUCT);
                                            } else if (status.equals("COMPLETED")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.COMPLETED);
                                            } else if (status.equals("PAUSE")) {
                                                orderBean.setStatus(CommonEnum.EngineeringOrderStatus.PAUSE);
                                            }
                                        }

                                        engineeringOrderBeanList.add(orderBean);

                                    }
                                }

                                teamMaintainOrderListener.getTeamMaintainOrderListSuceesed(engineeringOrderBeanList);

                            }


                        } else {

                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }

                            teamMaintainOrderListener.getTeamMaintainOrderListFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnTeamMaintainOrderListener {
        void getTeamMaintainOrderListSuceesed(List<OrderParentBean> orderParentBeanList);

        void getTeamMaintainOrderListFailed(String desc);
    }
}
